package com.hclc.yxb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hclc.entity.dto.SelectUserCustomersDto;
import com.hclc.entity.dvo.SelectUserCustomersDvo;
import com.hclc.entity.model.contans.ApiConst;
import com.hclc.entity.model.enums.IsDisableEnum;
import com.hclc.entity.model.enums.IsRealNameEnum;
import com.hclc.entity.model.enums.UserGenderEnum;
import com.hclc.entity.model.page.PageUtil;
import com.hclc.mapper.customize.SelectUserCustomerMapper;
import com.hclc.util.bean.BeanCopyUtils;
import com.hclc.util.date.DateUtil;
import com.hclc.util.poi.NumberUtil;
import com.hclc.yxb.ao.SelectUserCustomersAo;
import com.hclc.yxb.service.UserConsoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * ('-')
 * 用户接口相关实现
 *
 * @author ljj
 * @version 1.0
 * @date 2020/10/19 10:52
 */
@Slf4j
@Service
public class UserConsoleServiceImpl implements UserConsoleService {

    @Autowired
    private SelectUserCustomerMapper selectUserCustomerMapper;

    /**
     * 获取用户列表实现
     *
     * @param util
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.hclc.hxwy.console.dto.SelectUserCustomersDto>
     * @author ljj
     * @version 1.0
     * @date 2020/10/19 11:17
     */
    @Override
    public IPage<SelectUserCustomersDto> selectUserCustomers(PageUtil<SelectUserCustomersAo> util) {
        Page<SelectUserCustomersDvo> page = new Page<>(util.getCurrent(), ApiConst.PAGE_SIZE);
        SelectUserCustomersAo ao = util.getData();

        IPage<SelectUserCustomersDvo> iPage = selectUserCustomerMapper.selectUserCustomerPage(page, ao.getUserName(), ao.getUserPhone(), ao.getConsumptionSort(), DateUtil.getTimestampByLocalDate(ao.getStartTime()), DateUtil.getTimestampByLocalDate(ao.getEndTime()));

        List<SelectUserCustomersDto> retList = new ArrayList<>(iPage.getRecords().size());
        IPage<SelectUserCustomersDto> retPage = new Page<>(iPage.getCurrent(), ApiConst.PAGE_SIZE, iPage.getTotal());
        retPage.setRecords(retList);

        if (CollectionUtils.isEmpty(iPage.getRecords())) {
            return retPage;
        }


        List<String> userIds = iPage.getRecords().parallelStream().filter(s -> Objects.isNull(s.getReceiverAddress())).map(s -> s.getUserId()).collect(Collectors.toList());

        iPage.getRecords().stream().forEach(s -> {
            SelectUserCustomersDto dto = selectUserCustomersDto();
            copy(s, dto);
            // 金额
            dto.setBalance(intToDoubleString(s.getBalance()));
            // 总消费额
            dto.setSumConsumption(intToDoubleString(s.getSumConsumption()));
            // 冻结金额
            dto.setFreeze(intToDoubleString(s.getFreeze()));
            // 累计充值金额
            dto.setSumRechargeBalance(intToDoubleString(s.getSumRechargeBalance()));
            // 总收入金额
            dto.setSumIncomeAmount(intToDoubleString(s.getSumIncomeAmount()));
            retList.add(dto);
        });

        return retPage;
    }

    /**
     * 将int转换为double并转换为string
     *
     * @param data
     * @return java.lang.String
     * @author ljj
     * @version 1.0
     * @date 2020/10/20 20:24
     */
    private String intToDoubleString(Integer data) {
        return String.valueOf(NumberUtil.intToDouble(data));
    }

    /**
     * CV大法好
     *
     * @param s
     * @param object
     * @return void
     * @author ljj
     * @version 1.0
     * @date 2020/8/9 14:55
     */
    private void copy(Object s, Object object) {
        try {
            BeanCopyUtils.copyProperties(s, object);
        } catch (Exception e) {
            log.error("", e);
            return;
        }
    }


    /**
     * 初始化{SelectUserCustomersDto}
     *
     * @param
     * @return com.hclc.hxwy.dto.SelectUserCustomersDto
     * @author ljj
     * @version 1.0
     * @date 2020/10/25 10:36
     */
    private SelectUserCustomersDto selectUserCustomersDto() {
        SelectUserCustomersDto dto = new SelectUserCustomersDto();
        dto.setUserId(Strings.EMPTY);
        dto.setUserName(Strings.EMPTY);
        dto.setUserAccount(Strings.EMPTY);
        dto.setUserPhone(Strings.EMPTY);
        dto.setUserLogo(Strings.EMPTY);
        dto.setUserGender(UserGenderEnum.UNKNOWN);
        dto.setBirth(LocalDate.now());
        dto.setIsRealName(IsRealNameEnum.NO);
        dto.setIsDisable(IsDisableEnum.NO);
        dto.setSort(ApiConst.ZERO);
        dto.setLastLoginTime(LocalDateTime.now());
        dto.setCreateTime(LocalDateTime.now());
        dto.setAreaId(Strings.EMPTY);
        dto.setProvinceId(Strings.EMPTY);
        dto.setCityId(Strings.EMPTY);
        dto.setRegionFullName(Strings.EMPTY);
        dto.setUserAmountId(Strings.EMPTY);
        dto.setTransPass(Strings.EMPTY);
        dto.setBalance(Strings.EMPTY);
        dto.setSumConsumption(Strings.EMPTY);
        dto.setFreeze(Strings.EMPTY);
        dto.setSumRechargeBalance(Strings.EMPTY);
        dto.setSumOrderNum(ApiConst.ZERO);
        dto.setSumIncomeAmount(Strings.EMPTY);
        dto.setReceiverAddress(Strings.EMPTY);
        return dto;
    }

}
